export const copyText = (data) => {
	uni.setClipboardData({
		data: data.toString(),
		success: () => {
			uni.showToast({
				title: '复制成功',
				icon: 'none'
			})
		}
	})
}

export const debounce = (fn, wait = 800) => {
	let timeout = null;
	return function() {
		let context = this;
		let args = arguments;
		if (timeout) clearTimeout(timeout);
		let callNow = !timeout;
		timeout = setTimeout(() => {
			timeout = null;
		}, wait);
		if (callNow) fn.apply(context, args);
	};
}

const padStart = (num) => {
	return num.toString().padStart(2, 0)
}

export const getDate = (timeStr, format = 'YYYY-MM-DD HH:mm:SS') => {
	const date = new Date(Number(timeStr))
	if (format === 'YYYY-MM-DD HH:mm:SS') {
		return date.getFullYear() + '-' + padStart(date.getMonth() + 1) + '-' + padStart(date.getDate()) + ' ' +
			padStart(date.getHours()) +
			':' + padStart(date.getMinutes()) + ':' + padStart(date.getSeconds())
	} else if (format === 'YYYY-MM-DD') {
		return date.getFullYear() + '-' + padStart(date.getMonth() + 1) + '-' + padStart(date.getDate())
	} else {
		return {
			year: date.getFullYear(),
			month: padStart(date.getMonth() + 1),
			date: padStart(date.getDate()),
			hour: padStart(date.getHours()),
			minute: padStart(date.getMinutes()),
			second: padStart(date.getSeconds())
		}
	}
}